package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.alibaba.fastjson.JSON;

import dao.TeacherDao;
import entity.Exam;
import entity.Student;
import entity.Teacher;
import utils.DBManager;
import utils.JSONTools;

public class TeacherDaoImpl implements TeacherDao {

	@Override
	public boolean login(Teacher teacher) {
		String sql = "select * from teacher where number = ? and password = ?";
		try {
			Connection conn = DBManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setLong(1, teacher.getNumber());
			ps.setString(2, teacher.getPassword());
			ResultSet rs = ps.executeQuery();
			if (rs.next())
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean doInsert(Teacher teacher) {
		String sql = "insert into teacher(number,password,name) values (?,?,?)";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ps.setLong(1, teacher.getNumber());
			ps.setString(2, teacher.getPassword());
			ps.setString(3, teacher.getName());
			if (ps.executeUpdate() == 1)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean examInsert(Exam exam) {
		String sql = "insert into exam(name,starttime,endtime,examFile,submitFiles) values (?,?,?,?,?)";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ps.setString(1, exam.getName());
			ps.setString(2, exam.getStarttime());
			ps.setString(3, exam.getEndtime());
			ps.setString(4, exam.getExamFile());
			ps.setString(5, exam.getSubmitFiles());
			if (ps.executeUpdate() == 1)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public JSON findAllExam() {
		String sql = "select id,name,starttime,endtime,autostart,not_started,ongoing,ended from exam";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			JSON js = JSONTools.resultSetToJSON(rs);
			return js;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean beginExam(long id) {
		String sql = "update exam set not_started=0, ongoing=1, ended=0 where id = ?";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ps.setLong(1, id);
			if (ps.executeUpdate() == 1)
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean endExam(long id) {
		String sql = "update exam set not_started=0, ongoing=0, ended=1 where id = ?";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ps.setLong(1, id);
			if (ps.executeUpdate() == 1)
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean deleteExam(long id) {
		String sql = "delete from exam where id = ?";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ps.setLong(1, id);
			if (ps.executeUpdate() == 1)
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean examOngoing() {
		String sql = "select * from exam where ongoing = 1";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			if (rs.next())
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public JSON findAllStudent() {
		String sql = "select number,name from student";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			JSON js = JSONTools.resultSetToJSON(rs);
			return js;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean studentInsert(Student student) {
		String query = "select * from student where number = ?";
		String sql = "insert into student(number,name,password,clazz,teacherId) values (?,?,?,?,?)";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(query);
			ps.setLong(1, student.getNumber());
			ResultSet rs = ps.executeQuery();
			if(rs.next()) {
				return false;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		try {
			PreparedStatement ps2 = DBManager.getConnection().prepareStatement(sql);
			ps2.setLong(1, student.getNumber());
			ps2.setString(2, student.getName());
			ps2.setString(3, student.getPassword());
			ps2.setString(4, student.getClazz());
			ps2.setString(5, student.getTeacherId());
			if (ps2.executeUpdate() == 1)
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean unbindIP(Student student) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean sendMes(String mes) {
		// TODO Auto-generated method stub
		return false;
	}

}
